import java.util.Arrays;

public class Solution1706 {
    public int[] findBall(int[][] grid) {
        int[] ans=new int[grid[0].length];
        for (int i=0;i<grid[0].length;i++){
            int cur=i,row=0;
            while (row<grid.length){
                if (grid[row][cur]==1){
                    if (cur==grid[0].length-1 || grid[row][cur+1]==-1){
                        ans[i]=-1;
                        break;
                    }
                    cur++;
                }else{
                    if (cur==0 || grid[row][cur-1]==1){
                        ans[i]=-1;
                        break;
                    }
                    cur--;
                }
                row++;
            }
            if (ans[i]!=-1){
                ans[i]=cur;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution1706().findBall(new int[][]{{1, 1, 1, -1, -1}, {1, 1, 1, -1, -1}, {-1, -1, -1, 1, 1}, {1, 1, 1, 1, -1}})));
    }
}
